package com.multiplayer.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.multiplayer.entity.Seat;
import com.multiplayer.entity.StudyRoom;
import com.multiplayer.service.ISeatService;
import com.multiplayer.service.IStudyRoomService;
import com.multiplayer.service.impl.SeatServiceImpl;
import com.multiplayer.service.impl.StudyRoomServiceImpl;
import com.multiplayer.tools.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.groovy.GJson;





/**
 * Servlet implementation class SeatManageServlet
 */
@WebServlet("/SeatManageServlet")
public class SeatManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ISeatService seatService=new SeatServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeatManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 1设置请求编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setContentType("application/json");
				
				// 标记当前为何种操作的变量
				String op = "";
				if (request.getParameter("op") != null) {
					op = request.getParameter("op");
				}
				System.out.println("op:" + op);
		        if ("queryByPage".equals(op)) {
					// 分页显示数据
					doQueryByPage(request, response);
				}
		        else if ("editSeat".equals(op)) {
					// 修改座位信息
					try {
						doEditSeat(request, response);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		        else if ("deleteSeatByNum".equals(op)) {
					// 批量删除
					doDeleteSeatByNum(request, response);
					
				}
		        else if ("deleteSeatById".equals(op)) {
		 					// 单个座位删除
		 					doDeleteSeatById(request, response);
		 				}
		        else if ("addSeatByNum".equals(op)) {
					// 批量增加
					try {
						doAddSeatByNum(request, response);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
			private void doDeleteSeatById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				
				System.out.println("执行doDeleteSeatById");
				String deleSeatStr=request.getParameter("deleSeatStr");
				String seatList[]=deleSeatStr.split(",");
				for (int i = 0; i < seatList.length; i++) {
					String seatId=seatList[i];
					if(seatService.deleteSeatById(seatId))
					{System.out.println("删除成功");}
					else
					{System.out.println("删除失败");}
				}
				doQueryByPage(request, response);//刷新数据到页面
	}

			/**
			 * 增加自习室
			 * @param request
			 * @param response
			 * @throws ParseException 
			 * @throws IOException 
			 * @throws ServletException 
			 */
	
	private void doAddSeatByNum(HttpServletRequest request, HttpServletResponse response) throws ParseException, ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("执行doAddSeatByNum");
		String Rno=request.getParameter("studyRoomNo");
		int seatNum=0;
		if(request.getParameter("seatNum")!=null)
		{
			seatNum=Integer.parseInt(request.getParameter("seatNum"));
		}
		if(seatService.addSeat(seatNum, Rno))
		{
			System.out.println("添加成功");
		}
		else
		{
			System.out.println("添加失败");
		}
		doQueryByPage(request, response);//刷新数据到页面
	}

	/**
	 * 删除自习室
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
			private void doDeleteSeatByNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				System.out.println("执行doDeleteSeatByNum");
				String Rno=request.getParameter("studyRoomNo");
				int seatNum=0;
				if(request.getParameter("seatNum")!=null)
				{
					seatNum=Integer.parseInt(request.getParameter("seatNum"));
				}
				if(seatService.deleteSeatByNum(seatNum, Rno))
				{
					System.out.println("删除成功");
				}
				else
				{
					System.out.println("删除失败");
				}
				
				doQueryByPage(request, response);//刷新数据到页面
	}

			/**
			 * 修改自习室
			 * @param request
			 * @param response
			 * @throws ServletException
			 * @throws IOException
			 * @throws ParseException 
			 */
			private void doEditSeat(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
		// TODO Auto-generated method stub
				System.out.println("执行doEditStudyRoom");
				String Eseatno=request.getParameter("Eseatno");
				String Rno=request.getParameter("Rno");
				String Estatus=request.getParameter("Estatus");
				Seat seat=new Seat(Eseatno,Rno,Estatus);
				if(seatService.editSeat(seat))
				{
					System.out.println("修改成功");
				}
				else
				{
					System.out.println("修改失败");
				}
				doQueryByPage(request, response);//刷新数据到页面
	}

			/**
			 * 模糊查询 分页
			 * 
			 * @param request
			 * @param response
			 * @throws ServletException
			 * @throws IOException
			 */
			private void doQueryByPage(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
				System.out.println("执行doQueryByPage");
				String keywords = "";
				// 获取请求参数
				if (request.getParameter("keywords") != null) {
					keywords = request.getParameter("keywords");
				}
				// 页码默认值为1
				int page = 1;
				// 每页记录数默认为10
				int pageSize = 10;
				if (request.getParameter("page") != null) {
					page = Integer.parseInt(request.getParameter("page"));
				}

				if (request.getParameter("pageSize") != null) {
					pageSize = Integer.parseInt(request.getParameter("pageSize"));
				}

				// 3 调用 Service的方法
				PageData<Seat> pd = seatService.getSeatByPage(page, pageSize, keywords);
				JSONObject pageInfoObj = new JSONObject();
				pageInfoObj.put("page",pd.getPage());
				pageInfoObj.put("totalCount", pd.getTotalCount());
				pageInfoObj.put("pageSize", pd.getPageSize());
				pageInfoObj.put("totalPage", pd.getTotalPage());
				JSONArray  DataArray = new JSONArray();
				JSONArray  array = new JSONArray();
				
				for(Seat seat: pd.getData()){		
					JSONObject obj = new JSONObject();
					obj.put("Eseatno", seat.getEseatno());
					obj.put("Rno", seat.getRno());
					obj.put("Estatus",seat.getEstatus());
					array.add(obj);
				}
				System.out.println(array);
				DataArray.add(array);
				DataArray.add(pageInfoObj);
				PrintWriter out=response.getWriter();
				out.println(DataArray);//传json数据给前端
			}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
